/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.faces.convert.Converter;
import util.enumeration.Department;

/**
 *
 * @author WenYong
 */
@FacesConverter(value = "departmentConverter", forClass = util.enumeration.Department.class)
public class DepartmentConverter implements Converter {
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    
        if (value == null || value.length() == 0) {
            return null;
        }

        if(value.equals("INDUSTRY_LIAISON")) return Department.INDUSTRY_LIAISON;
        else if(value.equals("OVERSEAS_LIAISON")) return Department.OVERSEAS_LIAISON;
        else if(value.equals("TOURISM_MARKETING")) return Department.TOURISM_MARKETING;
        else if(value.equals("ADVERTISING")) return Department.ADVERTISING;
        else if(value.equals("STATISTICAL_ANALYSIS")) return Department.STATISTICAL_ANALYSIS;
        else if(value.equals("INFORMATION_TECHNOLOGY")) return Department.INFORMATION_TECHNOLOGY;
        else return Department.TOURIST_SERVICE;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null) {
            return null;
        }

        if (value instanceof Department) {
            Department department = (Department) value;

            if(department.equals(Department.INDUSTRY_LIAISON)) return "INDUSTRY_LIAISON";
            else if(department.equals(Department.OVERSEAS_LIAISON)) return "OVERSEAS_LIAISON)";
            else if(department.equals(Department.TOURISM_MARKETING)) return "TOURISM_MARKETING)";
            else if(department.equals(Department.ADVERTISING)) return "ADVERTISING";
            else if(department.equals(Department.STATISTICAL_ANALYSIS)) return "STATISTICAL_ANALYSIS";
            else if(department.equals(Department.INFORMATION_TECHNOLOGY)) return "INFORMATION_TECHNOLOGY";
            else return "TOURIST_SERVICE";
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.Department");
        }
    }
}
